using System.Data.Common;
using System.Xml.Serialization;
namespace FROG
{
    public class Database
    {
        private string _name;
        private string _dsn;
        private string _username;
        private string _password;
        private bool _autoCommit;
        private string _providerName = "System.Data.Odbc";

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string DSN
        {
            get { return _dsn; }
            set { _dsn = value; }
        }

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public bool AutoCommit
        {
            get { return _autoCommit; }
            set { _autoCommit = value; }
        }

        public DbConnection CreateDbConnection()
        {
            string connStr = string.Format(
                "Dsn={0};Uid={1};Pwd={2};Autocommit={3}",
                _dsn,
                _username,
                _password,
                _autoCommit ? "on" : "off");
            DbConnection dbConn = Factory.CreateConnection();
            dbConn.ConnectionString = connStr;
            return dbConn;
        }

        public OpenConnection CreateConnection()
        {
            OpenConnection oConn = new OpenConnection(this);
            return oConn;
        }

        public string ProviderName
        {
            get { return _providerName; }
            set { _providerName = value; }
        }

        [XmlIgnore]
        public DbProviderFactory Factory
        {
            get {
                return DbProviderFactories.GetFactory(_providerName);
            }
        }
    }
}
